package com.edaoe.module.api.admin;

import com.edaoe.interceptor.AuthInterceptor;
import com.edaoe.util.CaptchaUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

@Before(AuthInterceptor.class)
public class ApiAdminController extends Controller{
    private ApiAdminService service=new ApiAdminService();

    /**
     * 图片验证码
     */
    @Clear(AuthInterceptor.class)
    public void captcha() {
        renderJson(CaptchaUtil.captcha());
    }

    /**
     * 登录
     */
    @Clear(AuthInterceptor.class)
    public void login(){
        String username=getPara("username");
        String password=getPara("password");
        String captcha=getPara("captcha");
        String captchaToken=getPara("captchaToken");
        renderJson(service.login(username,password,captcha,captchaToken));
    }

    /**
     * 文章列表
     */
    public void articles(){
        long userId=getAttr("userId");
        int pageNumber=getParaToInt("pageNumber");
        int pageSize=getParaToInt("pageSize");
        renderJson(service.articles(userId,pageNumber,pageSize));
    }

    /**
     * 删除文章
     */
    public void deleteArticle(){
        long userId=getAttr("userId");
        long articleId=getParaToLong("articleId");
        renderJson(service.deleteArticle(userId,articleId));
    }

    /**
     * 编辑文章
     */
    public void editArticle(){
        long userId=getAttr("userId");
        long articleId=getParaToLong("articleId");
        renderJson(service.editArticle(userId,articleId));
    }

    /**
     * 获取分类
     */
    public void getCategories(){
        long userId=getAttr("userId");
        renderJson(service.getCategories(userId));
    }

    /**
     * 获取标签
     */
    public void getTags(){
        long userId=getAttr("userId");
        renderJson(service.getTags(userId));
    }

    /**
     * 保存文章
     */
    public void saveArticle(){
        long userId=getAttr("userId");
        Long articleId=getParaToLong("id");
        String title=getPara("title");
        int type=getParaToInt("type");
        String mdContent=getPara("mdContent");
        String htmlContent=getPara("htmlContent");
        Long[] categoryIds=getParaValuesToLong("categoryIds[]");
        String[] categoryNames=getParaValues("categoryNames[]");
        Long[] tagIds=getParaValuesToLong("tagIds[]");
        String[] tagNames=getParaValues("tagNames[]");
        renderJson(service.saveArticle(userId,articleId,title,type,mdContent,htmlContent,categoryIds,categoryNames,tagIds,tagNames));
    }

    /**
     * 分类列表
     */
    public void categories(){
        long userId=getAttr("userId");
        int pageNumber=getParaToInt("pageNumber");
        int pageSize=getParaToInt("pageSize");
        renderJson(service.categories(userId,pageNumber,pageSize));
    }

    /**
     * 删除分类
     */
    public void deleteCategory(){
        long userId=getAttr("userId");
        long categoryId=getParaToLong("categoryId");
        renderJson(service.deleteCategory(userId,categoryId));
    }

    /**
     * 保存分类
     */
    public void saveCategory(){
        long userId=getAttr("userId");
        Long categoryId=getParaToLong("id");
        String name=getPara("name");
        renderJson(service.saveCategory(userId,categoryId,name));
    }

    /**
     * 标签列表
     */
    public void tags(){
        long userId=getAttr("userId");
        int pageNumber=getParaToInt("pageNumber");
        int pageSize=getParaToInt("pageSize");
        renderJson(service.tags(userId,pageNumber,pageSize));
    }

    /**
     * 删除标签
     */
    public void deleteTag(){
        long userId=getAttr("userId");
        long tagId=getParaToLong("tagId");
        renderJson(service.deleteTag(userId,tagId));
    }

    /**
     * 保存标签
     */
    public void saveTag(){
        long userId=getAttr("userId");
        Long tagId=getParaToLong("id");
        String name=getPara("name");
        renderJson(service.saveTag(userId,tagId,name));
    }
    @Clear(AuthInterceptor.class)
    public void rebuildIndex(){
        service.rebuildIndex();
        renderText("ok");
    }
}
